//
//  YZWorkbenchVC.m
//  VET
//
//  Created by Lam BG on 2018/6/25.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZWorkbenchVC.h"
#import "YZDailyWorkVC.h"
#import "YZWorkNoteVC.h"
#import "AppDelegate.h"

#import "YZNumView.h"
#import "YZCardCell.h"

#import "YZUploadAPI.h"
#import "YZWorkbenchAPI.h"

#import "YZCardModel.h"
#import "YZWorkbenchModel.h"

#import "TBCityIconFont.h"

#import "UIView+Frame.h"
#import "UIColor+Category.h"
#import "UIImage+Gradient.h"
#import "UIImage+Category.h"
#import "YZRouter+AutoVC.h"
#import "UIViewController+BGTransition.h"

@interface YZWorkbenchVC () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topImgConst;


@property (weak, nonatomic) IBOutlet UIView *topBGView;
@property (weak, nonatomic) IBOutlet UIImageView *topImgView;

@property (weak, nonatomic) IBOutlet UIView *dailyBGView;
@property (weak, nonatomic) YZNumView *dailyNumView;

@property (weak, nonatomic) IBOutlet UIView *noteBGView;
@property (weak, nonatomic) YZNumView *noteNumView;



@property (nonatomic, strong) NSArray *titleArray;

@property (weak, nonatomic) IBOutlet UIView *bottomBGView;

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;

@property (nonatomic, strong) YZWorkbenchModel *wbModel;

@end

#define kCardCellID @"kCardCellID"

@implementation YZWorkbenchVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self setupTopView];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    // fix rootViewController 也能够接收右滑pop事件，导致界面卡死无法进入下一界面的情况。
    // 取消右滑返回
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    
    [self setupCollection];
    
    // 获取数据
    [[YZWorkbenchAPI index] subscribeNext:^(NSDictionary *data) {
        
        self.wbModel = [YZWorkbenchModel mj_objectWithKeyValues:data];
        [self reloadData];
        
    } error:^(NSError * _Nullable error) {
        
    }];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    // fix rootViewController 也能够接收右滑pop事件，导致界面卡死无法进入下一界面的情况。
    // 启用右滑返回
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}

#pragma mark - private fucntion

- (void)setupTopView {
    
    self.bg_navAlpha = 0;
    self.bg_navTitleColor = [UIColor whiteColor];
    
    // top 渐变色
    self.topImgView.image = [UIImage gradientFillSize:CGSizeMake(self.topImgView.width, self.topImgView.height) withFirstColor:yzColorGS andSecondColor:yzColorGE];
    
    // shadow
//    self.topBGView.layer.shadowOffset = CGSizeMake(0, 3);
//    self.topBGView.layer.shadowOpacity = 0.5;
//    self.topBGView.layer.shadowColor = [UIColor lightGrayColor].CGColor;
    
    // daily
    [self.dailyNumView updateNum:0];
    // note
    [self.noteNumView updateNum:0];
    
    
    // 小屏幕适配
    float topSpacing = -64;
    if (kIsiPhoneX) {
        
        topSpacing = -84;
    }
    
    self.topImgConst.constant = topSpacing;
}

- (void)setupCollection {
    
    if (self.flowLayout == nil) {
        
        // 配置 cell
        self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
        // 设置 item 大小
        float cellHeight = self.bottomBGView.size.height * .9;
        float cellWidth  = (2 * cellHeight) / 3;
        self.flowLayout.itemSize = CGSizeMake(cellWidth, cellHeight);
        // 设置UICollectionView为横向滚动
        self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.collectionView.showsHorizontalScrollIndicator = NO;
        self.flowLayout.minimumLineSpacing = 20;
        // 设置第一个cell和最后一个cell,与父控件之间的间距
        self.flowLayout.sectionInset = UIEdgeInsetsMake(10, 18, 0, 18);
        
        self.collectionView.collectionViewLayout = self.flowLayout;
        self.collectionView.dataSource = self;
        self.collectionView.delegate = self;
        
        // registe cell
        [self.collectionView registerNib:[UINib nibWithNibName:@"YZCardCell" bundle:nil] forCellWithReuseIdentifier:kCardCellID];
    }
}

- (void)reloadData {
    
    [self.dailyNumView updateNum:self.wbModel.taskNum];
    [self.noteNumView updateNum:self.wbModel.noteNum];
    
    // 新数据
    self.wbModel.swineCard.cardType  = WBCardTypeSwine;
    self.wbModel.herdCard.cardType   = WBCardTypeSwinery;
    self.wbModel.dieoutCard.cardType = WBCardTypeDieOut;
    
    self.titleArray = [@[self.wbModel.swineCard, self.wbModel.herdCard, self.wbModel.dieoutCard] sortedArrayUsingComparator:^NSComparisonResult(YZCardModel *obj1, YZCardModel *obj2) {
        
        NSInteger date1 = [obj1.updateTime integerValue];
        NSInteger date2 = [obj2.updateTime integerValue];
        
        return date1 < date2; // 升序
        
    }];

    [self.collectionView reloadData];
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:(UICollectionViewScrollPositionCenteredHorizontally) animated:YES];
}

#pragma mark - event actions

- (IBAction)dailyWorkClicked:(id)sender {
    
    // 每日待办
    YZDailyWorkVC *dwVC = [[YZDailyWorkVC alloc] init];
    [self.navigationController pushViewController:dwVC animated:YES];
}


- (IBAction)pushToWorkeNote:(id)sender {
    
    YZWorkNoteVC *noteVC = [[YZWorkNoteVC alloc] init];
    [self.navigationController pushViewController:noteVC animated:YES];
}

#pragma mark - collection delegate

// 固定 4 个
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.titleArray.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    YZCardModel *model = self.titleArray[indexPath.row];
    YZCardCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCardCellID forIndexPath:indexPath];
    [cell setupCard:model];
    
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
   
    YZCardModel *model = self.titleArray[indexPath.row];
    if (model.cardType == WBCardTypeSwine) {
        
        // 猪只健康卡
        [[YZRouter sharedInstance] pushVCName:@"YZPigHealthCardVC"];
        
    }else if (model.cardType == WBCardTypeSwinery){
        
        // 猪群健康卡
        [[YZRouter sharedInstance] pushVCName:@"YZSwineryHealthVC"];
        
    }else if(model.cardType == WBCardTypeDieOut){
        
        // 死淘卡
        [[YZRouter sharedInstance] pushVCName:@"YZDieOutVC"];
    }
}

#pragma mark - getter

- (NSArray *)titleArray {
    
    if (_titleArray == nil) {
        
        _titleArray = @[[YZCardModel initWithType:(WBCardTypeSwine)],
                        [YZCardModel initWithType:(WBCardTypeSwinery)],
                        [YZCardModel initWithType:(WBCardTypeDieOut)],
                        ];
        
    }
    
    return _titleArray;
}

- (YZNumView *)dailyNumView {
    
    if (_dailyNumView == nil) {
        
        YZNumView *numView = [[NSBundle mainBundle] loadNibNamed:@"YZNumView" owner:self options:nil].firstObject;
        numView.frame = self.dailyBGView.frame;
        numView.origin = CGPointMake(0, 0);
        [numView setNvType:(NumViewTypeDaily)];
        [self.dailyBGView addSubview:numView];
        _dailyNumView = numView;
    }
    
    return _dailyNumView;
}

- (YZNumView *)noteNumView {
    
    if (_noteNumView == nil) {
        
        YZNumView *numView = [[NSBundle mainBundle] loadNibNamed:@"YZNumView" owner:self options:nil].firstObject;
        numView.frame = self.noteBGView.frame;
        numView.origin = CGPointMake(0, 0);
        [numView setNvType:(NumViewTypeNote)];
        [self.noteBGView addSubview:numView];
        _noteNumView = numView;
    }
    
    return _noteNumView;
}

@end
















